<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\logic\LeescoreLogic;
use app\common\utils\Utils;
use think\Db;
use think\Request;


/**
 * Token接口
 */
class Leescore extends Api
{
    protected $noNeedLogin = ['index', 'indexGoods', 'getCategory', 'getGoodsList', 'search', 'detail'];
    protected $noNeedRight = '*';
    protected $shopLogic = null;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->shopLogic = new LeescoreLogic();
    }

    /**
     * 分类
     */
    public function getTreeArray() {
        $list = $this->shopLogic->getTreeArray();

        $this->success('分类列表', $list);
    }

    /**
     * 积分商城首页
     */
    public function index() {
        $Adszone = new \addons\adszone\library\Adszone();
        $banners = $Adszone->getAdsByMark('score_banner'); //按照标记调用广告位
        //首页3个分类
        $cates = $this->shopLogic->getCategory(3);

        $this->success('首页数据', [
            'banners' => $banners,
            'cate3'   => $cates,
        ]);
    }

    /**
     * 首页人气爆品和全部赠品接口
     */
    public function indexGoods() {
        $type = input('type/d', 0); //0为爆品 1为全部
        $where = [];
        if ($type == 0) {
            $where['flag'] = 1;
        }

        $list = $this->shopLogic->getGoodsList($where);

        $this->success('list', $list);
    }


    /**
     * 获取所有分类
     */
    public function getCategory() {
        $list = $this->shopLogic->getCategory();

        $this->success('所有分类', $list);
    }

    /**
     * 获取分类下商品
     *
     */
    public function getGoodsList() {
        $category_id = input('category_id/d', 0);
        $keyword = input('keyword/s', '');
        //键为排序字段 值为asc desc 为数组
        $orders = input('orders/a', []);
        $where = [];
        if ($keyword) {
            $where['name'] = ['like', "%$keyword%"];
        }
        if ($category_id) {
            $ids = $this->shopLogic->getChildIds($category_id);
            $where['category_id'] = ['in', $ids];
        }

        if ($orders) {
            $orderInner = $this->shopLogic->order;
            $order = array_merge($orders, $orderInner);
            $this->shopLogic->setOrder($order);
        }

        $list = $this->shopLogic->getGoodsList($where);
        $this->success('list', $list);
    }

    /**
     * 搜索
     */
    public function search() {
        //键为排序字段 值为asc desc 为数组
        $orders = input('orders/a', []);
        $keyword = input('keyword/s', '');
        if (!$keyword) {
            $this->success('无搜索参数', []);
        }
        $where['name'] = ['like', "%$keyword%"];
        $list = $this->shopLogic->setOrder($orders)->getGoodsList($where);

        $this->success('牛列表', $list);
    }


    /**
     * 商品详情
     */
    public function detail() {
        $id = input('id/d', 0);
        $detail = $this->shopLogic->detail($id);
        $pics = $detail['pics'];
        $arr = Utils::explodeStr($pics);
        foreach ($arr as $k => $v) {
            $arr[$k] = cdnurl($v, true);
        }
        if (empty($arr)) {
            $arr = [$detail->thumb_text];
        }
        $detail['pics'] = $arr;
        $detail['user_score'] = $this->auth->score;

        //增加浏览量
        Db::name('leescore_goods')->where('id', $id)->setInc('view_count', 1);
        $this->success('商品详情', $detail);
    }

    /**
     * 购物车生成订单
     */
    public function createOrder() {
        $order_id = $this->shopLogic->createOrder();
        $this->success('订单生成成功', $order_id);
    }

    /**
     * 立即购买
     */
    public function createOrderOne() {
        $order_id = $this->shopLogic->createOrderOne();
        $this->success('订单生成成功', $order_id);
    }
    /**
     * 购物车商品清单
     */
    public function postOrders() {
        $orderid = input('orderid/d', 0);
        $res = $this->shopLogic->postOrders($orderid);
        $this->success('商品清单', $res);
    }

    /** 获取不检测订单详情*/
    public function getOrders() {
        $orderid = input('orderid/d', 0);
        $res = $this->shopLogic->postOrders($orderid, false);
        $this->success('商品清单', $res);
    }
    /**
     * 检查库存
     */
    public function check() {
        $id = input('id/d', 0); //商品id
        $number = input('number/d', 0);

        $this->shopLogic->isCheck($id, $number);
        $this->success('检测成功');
    }

    /**支付*/
    public function pay() {
       $this->shopLogic->pay($this->auth);
        $this->success('支付成功');
    }

    /**
     * 加入购物车
     */
    public function joinCart() {
        $goods_id = input('goods_id/d', 0);
        $number = input('number/d', 0);

        if (!$goods_id || $goods_id <= 0) {
            $this->error('商品ID错误');
        }
        if ($number <= 0) {
            $this->error('商品数量不能小于0');
        }
        $cart = Db::name('leescore_cart');
        if ($cart->where(['uid' => UID, 'goods_id' => $goods_id])->count() > 0) {
            //修改时间
            $cart->where(['uid' => UID, 'goods_id' => $goods_id])->setField('createtime', time());
        } else {
            $cart->insert([
                'uid' => UID,
                'number' => $number,
                'goods_id' => $goods_id,
                'createtime' => time()
            ]);
        }

        $this->success('加入购物车成功');
    }

    /**
     * 我的购物车
     */

    public function cart() {
        //当前会员的购物车
        $cart = Db::name('leescore_cart');
        $list = $cart->alias('c')
            ->join('__LEESCORE_GOODS__ g', 'c.goods_id = g.id')
            ->order('c.createtime desc')
            ->where([
                'c.uid' => UID,
                'g.status' => 3
            ])
            ->field(['c.id', 'c.number', 'c.createtime','c.goods_id', 'g.status','g.name', 'g.scoreprice', 'g.thumb', 'g.money'])
            ->paginate()
            ->each(function ($item, $key) {
                $item['thumb_text'] = cdnurl($item['thumb'], true);
                return $item;
            });

        $this->success('我的购物车', $list);
    }

    /**
     * 从购物车删除
     */
    public function delete() {
        $this->shopLogic->delete();

        $this->success('从购物车删除成功');
    }

    public function setAddress() {
        $this->shopLogic->setAddress();

        $this->success('选择地址成功');
    }
}
